package com.axonactive.yolo.service.implement;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.axonactive.yolo.dao.interfaces.SubscribedFlightUserIdRepository;
import com.axonactive.yolo.dao.interfaces.UserRepository;
import com.axonactive.yolo.domain.SubscribedFlightUserId;
import com.axonactive.yolo.domain.User;
import com.axonactive.yolo.exception.SubcribedFlightUserIdNotFoundException;
import com.axonactive.yolo.service.interfaces.SubscribedFlightUserIdService;

@Service
public class SubscribedFlightUserIdServiceImpl implements
		SubscribedFlightUserIdService {

	@Resource
	private UserRepository userRepository;

	@Resource
	private SubscribedFlightUserIdRepository subscribedFlightUserIdRepository;

	@Transactional
//	@CacheEvict(value = "listSubscribedFlightUserIdCache", allEntries = true)
	public List<SubscribedFlightUserId> findAll() {
		return subscribedFlightUserIdRepository.findAll();
	}

	@Transactional
	public SubscribedFlightUserId create(
			SubscribedFlightUserId subcribedFlightUserId) {
		return subscribedFlightUserIdRepository.save(subcribedFlightUserId);
	}

	@Transactional
	public SubscribedFlightUserId delete(int id)
			throws SubcribedFlightUserIdNotFoundException {
		SubscribedFlightUserId deletedSubcribedFlight = subscribedFlightUserIdRepository
				.findOne(id);

		if (deletedSubcribedFlight == null)
			throw new SubcribedFlightUserIdNotFoundException(
					"Subcribed Flight UserID NotFoundException");

		subscribedFlightUserIdRepository.delete(deletedSubcribedFlight);
		return deletedSubcribedFlight;
	}

	@Transactional
	public SubscribedFlightUserId update(
			SubscribedFlightUserId subcribedFlightUserId)
			throws SubcribedFlightUserIdNotFoundException {
		// TODO Auto-generated method stub
		return null;
	}

	@Transactional
//	@Cacheable(value = "listSubscribedFlightUserIdCache", key = "#id")
	public SubscribedFlightUserId findById(int id) {
		return subscribedFlightUserIdRepository.findOne(id);
	}

	@Transactional
//	@Cacheable(value = "listSubscribedFlightUserIdCache", key = "#flightId")
	public List<SubscribedFlightUserId> findByFlightId(String flightId) {
		List<SubscribedFlightUserId> subscribedFlightUserIds = subscribedFlightUserIdRepository
				.findByFlightId(flightId);
		return subscribedFlightUserIds;
		// List<SubscribedFlightUserId> subscribedFlightUserIds =
		// subscribedFlightUserIdRepository
		// .findAll();
		//
		// Iterator<SubscribedFlightUserId> it =
		// subscribedFlightUserIds.iterator();
		// while (it.hasNext()) {
		// SubscribedFlightUserId subscribedFlightUserId = it.next();
		// if (subscribedFlightUserId.getFlightId() != flightId) {
		// it.remove();
		// }
		// }
		// return subscribedFlightUserIds;
	}

	@Transactional
//	@Cacheable(value = "listSubscribedFlightUserIdCache", key = "#id")
	public List<SubscribedFlightUserId> findByUserId(int id) {
		List<SubscribedFlightUserId> subscribedFlights = subscribedFlightUserIdRepository
				.findAll();

		Iterator<SubscribedFlightUserId> it = subscribedFlights.iterator();
		while (it.hasNext()) {
			SubscribedFlightUserId subcribedFlight = it.next();
			if (subcribedFlight.getUserId() != id) {
				it.remove();
			}
		}
		return subscribedFlights;
	}

	@Transactional
//	@Cacheable(value = "listSubscribedFlightUserIdCache", key = "")
	public SubscribedFlightUserId findByFlightIdAndUserId(String flightId,
			int userId) {
		List<SubscribedFlightUserId> subscribedFlights = subscribedFlightUserIdRepository
				.findByFlightIdAndUserId(flightId, userId);
		if (subscribedFlights == null || subscribedFlights.size() == 0)
			return null;
		return subscribedFlights.get(0);
	}

	@Transactional
	public List<Integer> findUserIdByFlightId(String flightId) {
		// List<SubscribedFlightUserId> subscribedFlights =
		// subscribedFlightUserIdRepository
		// .findByFlightId(flightId);

		return null;
	}

	@Transactional
//	@Cacheable(value = "listSubscribedFlightUserIdCache", key = "#flightId")
	public List<Integer> findUserIDsByFlightId(String flightId) {
		List<SubscribedFlightUserId> subscribedFlights = subscribedFlightUserIdRepository
				.findByFlightId(flightId);
		List<Integer> userIds = new ArrayList<>();
		for (Iterator iterator = subscribedFlights.iterator(); iterator
				.hasNext();) {
			SubscribedFlightUserId subUser = (SubscribedFlightUserId) iterator
					.next();
			userIds.add(subUser.getUserId());
		}
		return userIds;
	}

	@Transactional
//	@Cacheable(value = "listSubscribedFlightUserIdCache", key = "#flightId")
	public List<User> findUsersByFlightId(String flightId) {
		List<SubscribedFlightUserId> subscribedFlights = subscribedFlightUserIdRepository
				.findByFlightId(flightId);
		List<User> users = new ArrayList<>();
		for (Iterator iterator = subscribedFlights.iterator(); iterator
				.hasNext();) {
			SubscribedFlightUserId subUser = (SubscribedFlightUserId) iterator
					.next();
			User user = userRepository.findOne(subUser.getUserId());
			users.add(user);
		}
		return users;
	}

}
